package com.cash.app.ui.order.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import coil3.load
import coil3.request.placeholder
import com.cash.app.R
import com.cash.app.constant.AppConstant.OS_OVERDUE
import com.cash.app.constant.AppConstant.OS_UNDER_REVIEW
import com.cash.app.constant.AppConstant.OS_AUDIT_FAILED
import com.cash.app.constant.AppConstant.OS_PENDING_PAYMENT
import com.cash.app.constant.AppConstant.OS_FAILURE_PAYMENT
import com.cash.app.constant.AppConstant.OS_PENDING_REPAY
import com.cash.app.constant.AppConstant.OS_REPAID_REPAYMENT
import com.cash.app.constant.AppConstant.OS_MODIFY_BANK_INFO
import com.cash.app.databinding.AcLoanListItemBinding
import com.cash.app.manager.model.LoanOrderItem
import com.cash.app.utils.NumberUtils

class LoanListAdapter : RecyclerView.Adapter<LoanListAdapter.ViewHolder>() {

    private val items = mutableListOf<LoanOrderItem>()
    var onItemClickListener: ((LoanOrderItem) -> Unit)? = null

    @SuppressLint("NotifyDataSetChanged")
    fun setData(newItems: List<LoanOrderItem>) {
        items.clear()
        items.addAll(newItems)
        notifyDataSetChanged()
    }

    private fun getOrderStatusIcon(status: Int): Int {
        return when (status) {
            OS_UNDER_REVIEW -> R.mipmap.underreview
            OS_AUDIT_FAILED -> R.mipmap.auditfailed
            OS_PENDING_PAYMENT -> R.mipmap.pendingpayment
            OS_FAILURE_PAYMENT -> R.mipmap.paymentfailed
            OS_PENDING_REPAY -> R.mipmap.pendingrepayment
            OS_REPAID_REPAYMENT -> R.mipmap.repaidrepayment
            OS_OVERDUE -> R.mipmap.pendingrepayment // overdue use pending repayment
            OS_MODIFY_BANK_INFO -> R.mipmap.modifybankinfo
            else -> R.mipmap.app_icon
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = AcLoanListItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(binding)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(items[position])
    }

    override fun getItemCount(): Int = items.size

    inner class ViewHolder(private val binding: AcLoanListItemBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(item: LoanOrderItem) {
            with(binding) {

                if (item.p_icon.isNotEmpty()) {
                    productIcon.load(item.p_icon) {
                        placeholder(R.mipmap.app_icon)
                    }
                }
                productName.text = item.p_name
                repayAmount.text = NumberUtils.toRupeeWithComma(item.order_money)

                orderNumber.text = item.o_id.toString()
                dueDate.text = item.due_time_string

                val isOverdue = item.order_status == OS_OVERDUE
                overdueTag.visibility =
                    if (isOverdue) android.view.View.VISIBLE else android.view.View.GONE

                orderStatusIcon.load(getOrderStatusIcon(item.order_status))

                itemLoanCl.setOnClickListener {
                    onItemClickListener?.invoke(item)
                }
            }
        }
    }
} 